package centerberg.engine;

import java.awt.Dimension;
import java.util.Comparator;
import java.util.Iterator;
import java.util.PriorityQueue;

/**
 * A scene to be rendered by a render. Scenes should be created via the
 * renders {@link Render#createScene()} method in case the render requires
 * a special kind of scene.
 * 
 * When the scene is rendered the order in which the drawable objects are
 * rendered is determined by their size, biggest to smallest.
 * 
 * @author Andreas Svanberg
 */
public class Scene
{
    private PriorityQueue<Drawable> drawObjects;
    
    public Scene()
    {
        drawObjects = new PriorityQueue<Drawable>(11, sorter);
    }
    
    /**
     * Adds a drawable object to the scene.
     * 
     * @param the drawable object
     */
    public void add(Drawable object)
    {
        drawObjects.add(object);
    }
    
    /**
     * Returns an iterator of all the drawable objects.
     * 
     * @return iterator of the drawable objects
     */
    public Iterator<Drawable> drawObjects()
    {
        return drawObjects.iterator();
    }
    
    private static final Comparator<Drawable> sorter = new SizeComparator();
    
    private static class SizeComparator implements Comparator<Drawable>
    {
        public int compare(Drawable o1, Drawable o2)
        {
            Dimension o1_dim = o1.getTexture().getSize();
            Dimension o2_dim = o2.getTexture().getSize();
            
            int o1_size = o1_dim.width * o1_dim.height;
            int o2_size = o2_dim.width * o2_dim.height;
            
            // Render bigger textures first
            return o1_size > o2_size ? -1 : 1;
        }
    }
}